package com.example.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.config.ResponseBean;
import com.example.config.UnicomResponseEnums;
import com.example.sys.entity.UserRole;
import com.example.sys.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户和角色关联表 前端控制器
 * </p>
 *
 * @author anan
 * @since 2019-08-29
 */
@RestController
@RequestMapping("/sys/user-role")
public class UserRoleController {

    @Autowired
    private UserRoleService userRoleService;

    @PostMapping("/updateRoleToUser")
    public ResponseBean<UnicomResponseEnums> updateRoleToUser(@RequestBody Map<String,Object> map1){
        ArrayList<Object> arrayList = (ArrayList<Object>) map1.get("data");
        Long roleId=((Integer)map1.get("roleId")).longValue();
        Long sum;
        UserRole userRole;
        List<UserRole> list = new ArrayList<>();
        for (int i=0; i<arrayList.size();i++){
            userRole=new UserRole();
            Map<String,Object> map= (Map<String, Object>) arrayList.get(i);
            sum= ((Integer) map.get("userId")).longValue();
            userRole.setRoleId(roleId);
            userRole.setUserId(sum);
            list.add(userRole);
        }
        QueryWrapper<UserRole> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", roleId);
        userRoleService.remove(wrapper);
        userRoleService.saveBatch(list);
        return new ResponseBean(true, UnicomResponseEnums.SUCCESS_OPTION);
    }
}
